// CeCalcDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CeCalc.h"
#include "CeCalcDlg.h"
#include "constant.h"
#include "AboutDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCeCalcDlg dialog
CCeCalcDlg::CCeCalcDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCeCalcDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCeCalcDlg)
	m_rad = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCeCalcDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCeCalcDlg)
	DDX_Control(pDX, IDC_STATIC_DIS, m_edit);
	DDX_Control(pDX, IDC_STATIC_MSTORED, mIsStored);
	DDX_Radio(pDX, IDC_RADIO5, m_rad);
	//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCeCalcDlg, CDialog)
	//{{AFX_MSG_MAP(CCeCalcDlg)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
	ON_BN_CLICKED(IDC_DOT, OnDot)
	ON_BN_CLICKED(IDC_BUTTON0, OnButton0)
	ON_BN_CLICKED(IDC_SIGN, OnSign)
	ON_BN_CLICKED(IDC_RADIO5, OnRadioRAD)
	ON_BN_CLICKED(IDC_RADIO6, OnRadioDEG)
	ON_BN_CLICKED(IDC_RADIO_GRD, OnRadioGRD)
	ON_BN_CLICKED(IDC_BUTTON_RESET, OnButtonReset)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_SUB, OnSub)
	ON_BN_CLICKED(IDC_MULT, OnMult)
	ON_BN_CLICKED(IDC_DIV, OnDiv)
	ON_BN_CLICKED(IDC_CALC, OnCalc)
	ON_BN_CLICKED(IDC_SQRT, OnSqrt)
	ON_BN_CLICKED(IDC_BUTTON_RCEP, OnButtonRcep)
	ON_BN_CLICKED(IDC_MADD, OnMadd)
	ON_BN_CLICKED(IDC_INT, OnInt)
	ON_BN_CLICKED(IDC_MOD, OnMod)
	ON_BN_CLICKED(IDC_MS, OnMs)
	ON_BN_CLICKED(IDC_MR, OnMr)
	ON_BN_CLICKED(IDC_MC, OnMc)
	ON_BN_CLICKED(IDC_CONST_PI, OnConstPi)
	ON_BN_CLICKED(IDC_BUTTON_TAN, OnButtonTan)
	ON_BN_CLICKED(IDC_BUTTON_COS, OnButtonCos)
	ON_BN_CLICKED(IDC_SIN, OnSin)
	ON_BN_CLICKED(IDC_BUTTON_SQUARE, OnButtonSquare)
	ON_BN_CLICKED(IDC_CUBE, OnCube)
	ON_BN_CLICKED(IDC_POWER, OnPower)
	ON_BN_CLICKED(IDC_BUTTON_LOG, OnButtonLog)
	ON_BN_CLICKED(IDC_LN, OnLn)
	ON_BN_CLICKED(IDC_CONSTE, OnConstE)
	ON_BN_CLICKED(IDC_arcSIN2, OnarcSIN2)
	ON_BN_CLICKED(IDC_BUTTON_arcCOS2, OnBUTTONarcCOS2)
	ON_BN_CLICKED(IDC_BUTTON_arcTAN2, OnBUTTONarcTAN2)
	ON_BN_CLICKED(IDC_CLEAR_CUR, OnClearCur)
	ON_BN_CLICKED(IDC_BACKSPACE, OnBackspace)
	ON_COMMAND(ID_MENUITEMSIMPLE, OnMenuitemsimple)
	ON_COMMAND(ID_MENUITEMSCIENCE, OnMenuitemscience)
	ON_COMMAND(ID_MENUITEMABOUT, OnMenuitemabout)
	ON_COMMAND(ID_MENUITEMEXIT, OnMenuitemexit)
	ON_COMMAND(ID_MENUITEMRESET, OnMenuitemreset)
	ON_BN_CLICKED(IDC_CONST_E, OnFact)
	ON_BN_CLICKED(IDC_FACT, OnFact)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CCeCalcDlg message handlers
BOOL CCeCalcDlg::OnInitDialog(){
	CDialog::OnInitDialog();
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen
	
	// TODO: Add extra initialization here
	m_edit.SetWindowText(_T("0"));
	this->trigonometric=0;//default rad
	this->paboutDialog=NULL;
	HINSTANCE   g_hInst   =   ::AfxGetInstanceHandle(); 
	HWND   m_hWndCB; 
	m_hWndCB = CommandBar_Create(g_hInst,m_hWnd,1); 
	if   (m_hWndCB != NULL) 
	{ 
		//Add a menu bar 
		CommandBar_InsertMenubar(m_hWndCB,g_hInst,IDR_MENU1,0); 
		CommandBar_AddAdornments(m_hWndCB,0,0); 
    } 
	//////////////////////////////////////////////////////////////////////////
	RECT r;
	GetWindowRect(&r);
	this->width=r.right-r.left;
	this->height=r.bottom-r.top;
	this->SetWindowText(TEXT("WINCE 计算器"));
	return TRUE;  // return TRUE  unless you set the focus to a control
}
/*
 *	默认为弧度RAD
 */
void CCeCalcDlg::postMessage(int msg){
	postMessage(msg,RAD);
}
void CCeCalcDlg::postMessage(int msg,int para){
	CString _m("M"),_not_m("");
	int retValue;
	retValue=this->atm.acceptInstruction(msg,para);
	
	char tmp[64];
	strcpy(tmp,atm.getDisplay().c_str());
	CString t(tmp);
	m_edit.SetWindowText(t);
	switch (retValue)
	{
	case ST_UPDATE_MEMORY:
		if(atm.hasMemoryStored()){
			mIsStored.SetWindowText(atm.hasMemoryStored()?_m:_not_m);
		}
		break;
	case __EASTER_EGG__:
		MessageBox(_T("What a Genius,\nHow Can I beleive You Remember PI!")
			,_T("What a Genius"),MB_ICONASTERISK);
		break;
	default :break;
	}
	
}
void CCeCalcDlg::OnButton1(){
	postMessage('1');
	
}
void CCeCalcDlg::OnButton2(){
	postMessage('2');
	
}
void CCeCalcDlg::OnButton4(){
	postMessage('4');
	
}
void CCeCalcDlg::OnButton3(){
	postMessage('3');
	
}
void CCeCalcDlg::OnButton5(){
	postMessage('5');
}
void CCeCalcDlg::OnButton6(){
	postMessage('6');
}
void CCeCalcDlg::OnButton7(){
	postMessage('7');
}
void CCeCalcDlg::OnButton8(){
	postMessage('8');
}
void CCeCalcDlg::OnButton9(){
	postMessage('9');
}
void CCeCalcDlg::OnButton0(){
	postMessage('0');
}
void CCeCalcDlg::OnDot(){
	postMessage(kDot);
}
void CCeCalcDlg::OnSign(){
	postMessage(kSign);
}
void CCeCalcDlg::OnRadioRAD(){
	this->trigonometric=RAD;
}
void CCeCalcDlg::OnRadioDEG(){
	this->trigonometric=DEG;
}
void CCeCalcDlg::OnRadioGRD(){
	this->trigonometric=GRD;
}
//////////////////////////////////////////////////////////////////////////
void CCeCalcDlg::OnButtonReset(){
	this->postMessage(kReset);
}
void CCeCalcDlg::OnAdd(){
	
	this->postMessage(kAdd);	
}
void CCeCalcDlg::OnSub(){
	
	this->postMessage(kMinus);	
}
void CCeCalcDlg::OnMult(){
	
	this->postMessage(kMuliply);	
}
void CCeCalcDlg::OnDiv(){
	
	this->postMessage(kDivide);	
}
void CCeCalcDlg::OnMod(){
	this->postMessage(kMOD);	
}
void CCeCalcDlg::OnCalc(){
	
	this->postMessage(kCaculate);	
}
void CCeCalcDlg::OnSqrt(){
	this->postMessage(kSquareRoot);
	
}
void CCeCalcDlg::OnInt(){
	this->postMessage(kToInteger);
	
}
void CCeCalcDlg::OnButtonRcep(){
	this->postMessage(kReciprocal);
	
}
void CCeCalcDlg::OnMadd(){
	this->postMessage(kMemoryAdd);
	this->mIsStored.ShowWindow(SW_SHOW);
	
}
void CCeCalcDlg::OnMs(){
	
	this->postMessage(kMemorySave);
	this->mIsStored.ShowWindow(SW_SHOW);	
}
void CCeCalcDlg::OnMr(){
	
	this->postMessage(kMemoryRead);	
}
void CCeCalcDlg::OnMc(){
	
	this->postMessage(kMemoryClear);
	this->mIsStored.ShowWindow(SW_HIDE);
}
void CCeCalcDlg::OnConstPi(){
	this->postMessage(kConstantPI);
	
}
void CCeCalcDlg::OnButtonTan(){
	this->postMessage(kTangent,this->trigonometric);
	
}
void CCeCalcDlg::OnButtonCos(){
	this->postMessage(kCosine,this->trigonometric);
}
void CCeCalcDlg::OnSin(){
	this->postMessage(kSine,this->trigonometric);
}
void CCeCalcDlg::OnButtonSquare(){
	this->postMessage(kSquare);
}
void CCeCalcDlg::OnCube(){
	postMessage(kCube);		
}
void CCeCalcDlg::OnPower(){
	postMessage(kPower);	
}
void CCeCalcDlg::OnButtonLog(){
	postMessage(kLog10);	
}
void CCeCalcDlg::OnLn(){
	postMessage(kLn);	
}
void CCeCalcDlg::OnConstE(){
	postMessage(kConstantE);
}
void CCeCalcDlg::OnarcSIN2(){
	this->postMessage(kArcSine,this->trigonometric);
	
}
void CCeCalcDlg::OnBUTTONarcCOS2(){
	this->postMessage(kArcCosine,this->trigonometric);
}
void CCeCalcDlg::OnBUTTONarcTAN2(){
	this->postMessage(kArcTangent,this->trigonometric);
}
void CCeCalcDlg::OnClearCur() {
	this->postMessage(kClearCurrent);
}
void CCeCalcDlg::OnBackspace() {
	this->postMessage(kBackSpace);
}
void CCeCalcDlg::OnMenuitemsimple() {
	RECT r;
	GetWindowRect(&r);
	this->SetWindowPos(NULL,r.left,r.top,width/2,height,SWP_DRAWFRAME);
	this->postMessage(kReset);
}
void CCeCalcDlg::OnMenuitemscience() {
	RECT r;
	GetWindowRect(&r);
	this->SetWindowPos(NULL,r.left,r.top,width,height,SWP_DRAWFRAME);
	this->postMessage(kReset);
}
void CCeCalcDlg::OnMenuitemabout() {
	if (paboutDialog==NULL)
	{
		paboutDialog=new CAboutDialog();
	}
	paboutDialog->DoModal();
}
void CCeCalcDlg::OnMenuitemexit() {
	/*
	 *	释放资源
	 */
	if (this->paboutDialog)	{
		delete paboutDialog;
	}
	exit(0);
}
void CCeCalcDlg::OnMenuitemreset(){
	this->postMessage(kReset);
	this->postMessage(kMemoryClear);
	this->mIsStored.ShowWindow(SW_HIDE);
}
void CCeCalcDlg::OnFact(){
	postMessage(kFactorial);
}